package com.zengde.ai_sandbox.factory;

import com.zengde.ai_sandbox.service.strategy.PaymentStrategy;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class PaymentStrategyFactory {
    // Spring会自动将PaymentStrategy所有实现的Bean注入到这个Map中
    // key: Bean的名称 (如 ‘alipayPayment‘, ’wechatPayment‘)
    // value: PaymentStrategy接口的实例
    private final Map<String, PaymentStrategy> strategyMap;

    // 通过构造器注入
    public PaymentStrategyFactory(Map<String, PaymentStrategy> strategyMap) {
        this.strategyMap = strategyMap;
    }

    // 提供一个公共方法，根据类型获取策略
    public PaymentStrategy getStrategy(String type) {
        // 这里可以稍微优化一下，遍历Map，根据getType()来查找
        // 因为Bean的名称和业务类型可能不完全一样
        for (PaymentStrategy strategy : strategyMap.values()) {
            if (strategy.getType().equalsIgnoreCase(type)) {
                return strategy;
            }
        }
        throw new RuntimeException("Payment strategy not found for type: " + type);
    }

    // 或者更直接一点，如果你用Bean的名字作为key，可以直接get
    // public PaymentStrategy getStrategyByBeanName(String beanName) {
    //    if (strategyMap.containsKey(beanName)) {
    //        return strategyMap.get(beanName);
    //    }
    //    throw new RuntimeException("Strategy not found: " + beanName);
    // }
}